<template>
  <el-dialog
    :title="!dataForm.mhBh ? '病虫信息录入' : '病虫信息录入'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="编号" prop="mhBh" v-if="dataForm.mhBh">
            <el-input v-model="dataForm.mhBh" placeholder="编号" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="总株数" prop="mhNumber"  v-if="dataForm.mhBh">
            <el-input v-model="dataForm.mhNumber" placeholder="总株数" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="螨害株数" prop="bcMhzs"  v-if="dataForm.mhBh">
            <el-input v-model="dataForm.bcMhzs" placeholder="螨害株数" disabled></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    <el-row>
      <el-col :span="8">
        <el-form-item label="螨害率(%)" prop="bcTjmhzl"  v-if="dataForm.mhBh">
          <el-input v-model="dataForm.bcTjmhzl" placeholder="统计螨害株率(%)" disabled></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="平均级数" prop="bcPjmhjs"  v-if="dataForm.mhBh">
          <el-input v-model="dataForm.bcPjmhjs" placeholder="平均螨害级数" disabled></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="有螨株率(%)" prop="bcYmzl"  v-if="dataForm.mhBh" style = "white-space:nowrap;">
          <el-input v-model="dataForm.bcYmzl" placeholder="有螨株率(%)" disabled></el-input>
        </el-form-item>
      </el-col>
    </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="百株卵数" prop="bcBzls"  v-if="dataForm.mhBh" style = "white-space:nowrap;">
            <el-input v-model="dataForm.bcBzls" placeholder="百株卵数(只)" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="总虫量" prop="bcZcl"  v-if="dataForm.mhBh">
            <el-input v-model="dataForm.bcZcl" placeholder="总虫量(只)" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="有虫株率(%)" prop="bcYczl"  v-if="dataForm.mhBh" style = "white-space:nowrap;">
            <el-input v-model="dataForm.bcYczl" placeholder="有虫株率(%)" disabled></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    <el-row>
      <el-col :span="6">
        <el-form-item label="百株螨量" prop="bcBzml">
          <el-input v-model="dataForm.bcBzml" placeholder="百株螨量(只)"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="一级叶数" prop="bcYjmhys">
          <el-input v-model="dataForm.bcYjmhys" placeholder="一级螨害叶数(片)"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="二级叶数" prop="bcEjmhys">
          <el-input v-model="dataForm.bcEjmhys" placeholder="二级螨害叶数(片)"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="螨害级别" prop="bcMhjb">
          <el-input v-model="dataForm.bcMhjb" placeholder="螨害级别(1/2/3)"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
  <el-row>
    <el-col :span="6">
      <el-form-item label="卵数" prop="bcLs">
        <el-input v-model="dataForm.bcLs" placeholder="卵数(只)"></el-input>
      </el-form-item>
    </el-col>
    <el-col :span="6">
      <el-form-item label="幼虫数" prop="bcYcs">
        <el-input v-model="dataForm.bcYcs" placeholder="幼虫数(只)"></el-input>
      </el-form-item>
    </el-col>
    <el-col :span="6">
      <el-form-item label="龄期" prop="bcLq">
        <el-input v-model="dataForm.bcLq" placeholder=" 1 龄、2 龄、3 龄"></el-input>
      </el-form-item>
    </el-col>
    <el-col :span="6">
      <el-form-item label="百株铃虫" prop="bcBzlcl">
        <el-input v-model="dataForm.bcBzlcl" placeholder="百株铃虫量(只)"></el-input>
      </el-form-item>
    </el-col>
  </el-row>
   <el-row>
     <el-col :span="6">
       <el-form-item label="有蚜株率%" prop="bcYyzl" style = "white-space:nowrap;">
         <el-input v-model="dataForm.bcYyzl" placeholder="有蚜株率(%"></el-input>
       </el-form-item>
     </el-col>
     <el-col :span="6">
       <el-form-item label="百株蚜量" prop="bcBzyl">
         <el-input v-model="dataForm.bcBzyl" placeholder="百株蚜量(只)"></el-input>
       </el-form-item>
     </el-col>
   </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          hh: '',
          bcId: 0,
          mhBh: '',
          mhNumber: '',
          bcMhzs: '',
          bcBzml: '',
          bcYjmhys: '',
          bcEjmhys: '',
          bcTjmhzl: '',
          bcMhjb: '',
          bcPjmhjs: '',
          bcYmzl: '',
          bcLs: '',
          bcYcs: '',
          bcLq: '',
          bcBzls: '',
          bcBzlcl: '',
          bcZcl: '',
          bcYczl: '',
          bcYyzl: '',
          bcBzyl: ''
        },
        dataRule: {
          bcBzml: [
            { required: true, message: '百株螨量不能为空', trigger: 'blur' }
          ],
          bcYjmhys: [
            { required: true, message: '一级螨害叶数不能为空', trigger: 'blur' }
          ],
          bcEjmhys: [
            { required: true, message: '二级螨害叶数不能为空', trigger: 'blur' }
          ],
          bcMhjb: [
            { required: true, message: '螨害级别不能为空', trigger: 'blur' }
          ],
          bcLs: [
            { required: true, message: '卵数不能为空', trigger: 'blur' }
          ],
          bcYcs: [
            { required: true, message: '幼虫数不能为空', trigger: 'blur' }
          ],
          bcLq: [
            { required: true, message: '龄期如 1 龄、2 龄、3 龄等。不能为空', trigger: 'blur' }
          ],
          bcBzlcl: [
            { required: true, message: '百株铃虫量不能为空', trigger: 'blur' }
          ],
          bcYyzl: [
            { required: true, message: '有蚜株率不能为空', trigger: 'blur' }
          ],
          bcBzyl: [
            { required: true, message: '百株蚜量不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (mhBh) {
        console.log('mhbh' + mhBh)
        this.dataForm.mhBh = mhBh || ''
        this.hh = mhBh
        console.log('mhbh' + this.dataForm.mhBh)
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.mhBh) {
            this.$http({
              url: this.$http.adornUrl(`/mh/bc/infobh/${mhBh}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                console.log('病虫回显', data)
                this.dataForm.mhBh = mhBh
                this.dataForm.mhNumber = data.bc.mhNumber
                this.dataForm.bcMhzs = data.bc.bcMhzs
                this.dataForm.bcBzml = data.bc.bcBzml
                this.dataForm.bcYjmhys = data.bc.bcYjmhys
                this.dataForm.bcEjmhys = data.bc.bcEjmhys
                this.dataForm.bcTjmhzl = data.bc.bcTjmhzl
                this.dataForm.bcMhjb = data.bc.bcMhjb
                this.dataForm.bcPjmhjs = data.bc.bcPjmhjs
                this.dataForm.bcYmzl = data.bc.bcYmzl
                this.dataForm.bcLs = data.bc.bcLs
                this.dataForm.bcYcs = data.bc.bcYcs
                this.dataForm.bcLq = data.bc.bcLq
                this.dataForm.bcBzls = data.bc.bcBzls
                this.dataForm.bcBzlcl = data.bc.bcBzlcl
                this.dataForm.bcZcl = data.bc.bcZcl
                this.dataForm.bcYczl = data.bc.bcYczl
                this.dataForm.bcYyzl = data.bc.bcYyzl
                this.dataForm.bcBzyl = data.bc.bcBzyl
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/mh/bc/${!this.dataForm.bcId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'bcId': this.dataForm.bcId || undefined,
                'mhBh': this.dataForm.mhBh,
                'mhNumber': this.dataForm.mhNumber,
                'bcMhzs': this.dataForm.bcMhzs,
                'bcBzml': this.dataForm.bcBzml,
                'bcYjmhys': this.dataForm.bcYjmhys,
                'bcEjmhys': this.dataForm.bcEjmhys,
                'bcTjmhzl': this.dataForm.bcTjmhzl,
                'bcMhjb': this.dataForm.bcMhjb,
                'bcPjmhjs': this.dataForm.bcPjmhjs,
                'bcYmzl': this.dataForm.bcYmzl,
                'bcLs': this.dataForm.bcLs,
                'bcYcs': this.dataForm.bcYcs,
                'bcLq': this.dataForm.bcLq,
                'bcBzls': this.dataForm.bcBzls,
                'bcBzlcl': this.dataForm.bcBzlcl,
                'bcZcl': this.dataForm.bcZcl,
                'bcYczl': this.dataForm.bcYczl,
                'bcYyzl': this.dataForm.bcYyzl,
                'bcBzyl': this.dataForm.bcBzyl
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
